﻿using Levfier.Proccessors.Parameters;
using System;

namespace Levfier.Proccessors.Clippers
{
    /// <summary>
    /// ハードクリップ
    /// </summary>
    [Serializable]
    public class HardClipper : Proccessor
    {
        /// <summary>
        /// ゲイン
        /// </summary>
        private ProccessParameter gainParam;

        /// <summary>
        /// 入力を歪ませる（ハード）する
        /// </summary>
        /// <param name="input">入力</param>
        /// <param name="output">出力</param>
        protected override void InnerProccess(double[] inputBuffer, double[] outputBuffer)
        {
            //パラメータを取り出す
            double gain = ProccessParameter.ConvertDBToValue(gainParam.Value);

            double temp;

            //計算
            for (int i = 0; i < inputBuffer.Length; i++)
            {
                temp = gain * inputBuffer[i];

                if (temp > 1.0d)
                {
                    temp = 1.0d;
                }
                else if (temp < -1.0d)
                {
                    temp = -1.0d;
                }
                outputBuffer[i] = temp;
            }
        }

        /// <summary>
        /// パラメータを初期化する
        /// </summary>
        protected override void InitParameters()
        {
            //ゲイン
            this.gainParam = new ProccessParameter();
            this.gainParam.Name = "Gain[dB]";
            this.gainParam.Value = 0.0d;
            this.gainParam.MaxValue = 100.0d;
            this.gainParam.MinValue = -100.0d;
            this.gainParam.Step = 1.0d;
            this.parameters.Add(this.gainParam.Name, this.gainParam);
        }
    }
}
